
-- Farming Redo check

if farming and not farming.mod then
	print("[MyMelon] Farming Redo not found!")
	return
end

farming_stem_growth = {
	path = minetest.get_modpath("mymelon"),
}

dofile(farming_stem_growth.path .. "/stem_growth.lua")

-- Translation support

local S = minetest.get_translator("mymelon")

-- Melon seed

minetest.register_node("mymelon:seed_melon", {
	description = S("Melon Seed"),
	tiles = {"crops_melon_seed.png"},
	inventory_image = "crops_melon_seed.png",
	wield_image = "crops_melon_seed.png",
	drawtype = "signlike",
	groups = {seed = 1, snappy = 3, attached_node = 1, flammable = 4, growing = 1},
	paramtype = "light",
	paramtype2 = "wallmounted",
	walkable = false,
	sunlight_propagates = true,
	selection_box = farming.select,
	next_plant = "mymelon:melon_1",
	-- Place Melon Seed, or you can go directly to stage_1 of crop
	on_place = function(itemstack, placer, pointed_thing)
		return farming.place_seed(itemstack, placer, pointed_thing, "mymelon:seed_melon")
	end,
	-- The following function is only required if seed was placed instead of stage_1
	on_timer = function(pos, elapsed)
		minetest.set_node(pos, {name = "mymelon:melon_1", param2 = 1})
	end
})

-- Melon definition

local def = {
	drawtype = "plantlike",
	tiles = {"crops_melon_plant_1.png"},
	paramtype = "light",
	sunlight_propagates = true,
	walkable = false,
	buildable_to = true,
	drop = "",
	selection_box = farming.select,
	groups = {
		handy = 1, snappy = 3, flammable = 2, plant = 1, attached_node = 1,
		not_in_creative_inventory = 1, growing = 1
	},
	sounds = farming.sounds.node_sound_leaves_defaults()
}

-- Crop nodes

minetest.register_node("mymelon:melon_1", table.copy(def)) -- Stage 1

def.tiles = {"crops_melon_plant_2.png"}
minetest.register_node("mymelon:melon_2", table.copy(def)) -- Stage 2

def.tiles = {"crops_melon_plant_3.png"}
minetest.register_node("mymelon:melon_3", table.copy(def)) -- Stage 3

def.tiles = {"crops_melon_plant_4.png"}
minetest.register_node("mymelon:melon_4", table.copy(def)) -- Stage 4

def.tiles = {"crops_melon_plant_5.png"}
minetest.register_node("mymelon:melon_5", table.copy(def)) -- Stage 5

-- Melon stem

minetest.register_node("mymelon:melon_6", {
	visual = "mesh",
	mesh = "crops_plant_extra_face.obj",
	description = S("Melon plant"),
	tiles = {"crops_melon_stem.png", "crops_melon_plant_4.png"},
	drawtype = "mesh",
	paramtype2 = "facedir",
	sunlight_propagates = true,
	use_texture_alpha = "blend",
	walkable = false,
	paramtype = "light",
	groups = {
		handy = 1, snappy = 3, flammable = 3, attached_node = 1,
		not_in_creative_inventory = 1
	},
	drop = "mymelon:seed_melon",
	sounds = farming.sounds.node_sound_leaves_defaults(),
	on_construct = farming_stem_growth.stem_on_construct("mymelon:melon_6", "mymelon:melon", "mymelon:melon_5")
})


minetest.register_craftitem("mymelon:melon_slice", {
	description = S("Melon Slice"),
	inventory_image = "crops_melon_slice.png",
	groups = {food_melon_slice = 1, compostability = 48},
	on_use = minetest.item_eat(1)
})

minetest.register_craft({
	output = "mymelon:seed_melon",
	recipe = {{"mymelon:melon_slice"}}
})

-- Melon block

minetest.register_node("mymelon:melon", {
	description = S("Melon"),
	tiles = {
		"crops_melon_top.png",
		"crops_melon_bottom.png",
		"crops_melon.png",
	},
	sunlight_propagates = false,
	use_texture_alpha = "clip",
	walkable = true,
	groups = {
		handy = 1, snappy = 3, flammable = 3, oddly_breakable_by_hand = 2,
		food_melon = 1, compostability = 85
	},
	paramtype2 = "facedir",
	drop = "mymelon:melon",
	sounds = farming.sounds.node_sound_wood_defaults(),
	on_dig = farming_stem_growth.gourd_on_dig("mymelon:melon_6", "mymelon:melon_4")
})

-- Add to registered_plants

farming.registered_plants["mymelon:melon"] = {
	crop = "mymelon:melon",
	seed = "mymelon:seed_melon",
	minlight = 11,
	maxlight = 15,
	steps = 6
}

-- Add to world

local spawn_on = "default:dirt_with_grass"

if minetest.get_modpath("mcl_core") then
	spawn_on = "mcl_core:dirt_with_grass"
end

minetest.register_decoration({
	deco_type = "schematic",
	place_on = {spawn_on},
	sidelen = 16,
	fill_ratio = 0.01,
	y_min = 1,
	y_max = 5,
	schematic = farming_stem_growth.schematic("mymelon:melon_6", "mymelon:melon"),
	flags = "place_center_x, place_center_z",
	spawn_by = spawn_on,
	num_spawn_by = 8, -- make sure melon has enough space for stem
	rotation = "random"
})

-- Bonemeal support

if minetest.get_modpath("bonemeal") then
	bonemeal:add_crop({{"mymelon:melon_", 6, "mymelon:seed_melon"}})
end
